package com.masterclient.app.widget;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.gyf.immersionbar.ImmersionBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @auther hjian
 * @date 2020/6/8.
 */
public class GuideVipDialog extends Dialog {

    @BindView(R.id.dialog_guide_vip_image)
    ImageView dialogGuideVipImage;
    @BindView(R.id.dialog_guide_vip_btn)
    ImageView dialogGuideVipBtn;
    @BindView(R.id.dialog_guide_vip_root)
    View dialogGuideVipRoot;

    Activity context;
    int statusHeight, clickIndex, noScrollViewY;
    int[] imagesMarginTop ;
    int[] images = {R.mipmap.guide_vip_one, R.mipmap.guide_vip_two, R.mipmap.guide_vip_three, R.mipmap.guide_vip_four};

    public ImageYCallback imageYCallback;
    public interface ImageYCallback {
        void onImageYCallback(int index);
    }

    public GuideVipDialog(@NonNull Activity context, int statusHeight,
                          int[] imagesMarginTop, ImageYCallback imageYCallback) {
        super(context, R.style.MyDialog);
        this.context = context;
        this.statusHeight = statusHeight;
        this.imagesMarginTop = imagesMarginTop;
        this.imageYCallback = imageYCallback;
        clickIndex = 0;
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowparams = window.getAttributes();
        windowparams.width = ScreenUtils.getScreenWidth();
        windowparams.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawableResource(R.color.transparent);
        window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.setAttributes(windowparams);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_guide_vip);
        ButterKnife.bind(this);
        ImmersionBar.with(context,this).transparentStatusBar().fitsSystemWindows(false).init();
        setCanceledOnTouchOutside(true);
//        dialogGuideVipImage.setAlpha(0.3f);

        ((FrameLayout.LayoutParams)dialogGuideVipRoot.getLayoutParams())
                .setMargins(0, ScreenUtils.dp2px(78)+statusHeight,0, ScreenUtils.dp2px(78)+statusHeight);

        Glide.with(context).load(images[clickIndex]).apply(new RequestOptions()
                .error(R.color.transparent).placeholder(R.color.transparent)).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(@NonNull Drawable resource, Transition<? super Drawable> transition) {
                dialogGuideVipImage.setImageDrawable(resource);
            }
        });


    }


    @OnClick(R.id.dialog_guide_vip_btn)
    public void onViewClicked() {
        if (clickIndex == 3) {
            dismiss();
            return;
        }
        ++clickIndex;
        dialogGuideVipImage.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        Glide.with(context).load(images[clickIndex]).apply(new RequestOptions().centerCrop()
                .error(R.color.transparent).placeholder(R.color.transparent)).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(@NonNull Drawable resource, Transition<? super Drawable> transition) {
                dialogGuideVipRoot.setPadding(ScreenUtils.dp2px(16), imagesMarginTop[clickIndex-1],ScreenUtils.dp2px(16),0);
                imageYCallback.onImageYCallback(clickIndex);
                dialogGuideVipImage.setImageDrawable(resource);
            }
        });
    }
}
